package com.zhan.ielts.web.controller;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhan.frame.core.dao.BaseService;
import com.zhan.frame.core.utils.AliyunOssUtil;
import com.zhan.frame.core.web.controller.BaseControllerImpl;
import com.zhan.frame.core.web.domain.CustomResult;
import com.zhan.ielts.domain.AudioAnswer;
import com.zhan.ielts.domain.query.AudioAnswerQuery;
import com.zhan.ielts.service.AudioAnswerService;

@Controller
@RequestMapping("/audio/answer")
public class AudioAnswerController extends BaseControllerImpl<AudioAnswer, AudioAnswerQuery> {

	private Logger log = LoggerFactory.getLogger(AudioAnswerController.class);

	@Autowired
	private AudioAnswerService audioAnswerService;

	// @Autowired
	// private CommunityShareService communityShareService;

	@Override
	protected BaseService<AudioAnswer> getBaseService() {
		return audioAnswerService;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ResponseBody
	@RequestMapping(value = "/fetch", method = RequestMethod.POST)
	public CustomResult fetchAudioAnswer(AudioAnswer entity) {
		log.debug("获取音频信息开始");
		Map map = new HashMap();

		if (entity.getUserId() == null || entity.getUserId().trim().equals("")) {
			log.debug("userId为空！");
			map.put("errCode", 1001);
			return new CustomResult(0, "userId为空！", map);
		}
		try {
			map = audioAnswerService.fetchAudios(entity);
		} catch (Exception e) {
			log.debug("网络或系统错误！");
			map.put("errCode", 1002);
			return new CustomResult(0, "系统错误！", map);
		}
		log.debug("获取音频信息结束");
		return new CustomResult(1, "", map);
	}

	@SuppressWarnings({ "unused", "unchecked" })
	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public CustomResult updateAudioAnswer(String userId, String answer) {
		log.debug("音频修改开始");
		Map map = new HashMap();
		JSONObject jsonObject = JSONObject.fromObject(answer);

		AudioAnswer entity = (AudioAnswer) JSONObject.toBean(jsonObject, AudioAnswer.class);

		if (userId == null || userId.trim().equals("")) {
			log.debug("userId不允许为空！");
			map.put("errCode", 1001);
			return new CustomResult(0, "userId为空！", map);
		} else if (entity.getAudioId() == null || entity.getAudioId().trim().equals("")) {
			log.debug("audiosId不允许为空");
			map.put("errCode", 1002);
			return new CustomResult(0, "audiosId不允许为空！", map);
		}

		try {
			String speaking = "speaking";
			String writing = "writing";

			entity.setUserId(userId);
			audioAnswerService.updateAudios(entity);

		} catch (Exception e) {
			log.debug("网络或系统错误！");
			map.put("errCode", 1003);
			return new CustomResult(0, "系统错误！", map);
		}
		log.debug("音频修改结束");
		return new CustomResult(1, "", map);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@ResponseBody
	@RequestMapping(value = "/remove", method = RequestMethod.POST)
	public CustomResult removeAudios(AudioAnswer entity, String key) {
		log.debug("音频删除开始");
		Map map = new HashMap();
		if (entity.getUserId() == null || entity.getAudioId() == null || entity.getUserId().trim().equals("")
				|| entity.getAudioId().trim().equals("")) {
			log.debug("音频删除的参数错误！");
			map.put("errCode", 1001);
			return new CustomResult(0, "音频删除的参数错误", map);
		}

		try {
			AliyunOssUtil.deleteFile(key);
		} catch (Exception e) {
			log.debug("删除阿里云上的音频错误" + e);
			map.put("errCode", 1002);
			return new CustomResult(0, "删除阿里云上的音频错误！", map);
		}
		try {
			audioAnswerService.updateByCustom(entity);
			// communityShareService.deleteByAudio(entity.getAudioId());

		} catch (Exception e) {
			log.debug("系统错误！" + e);
			map.put("errCode", 1003);
			return new CustomResult(0, "系统错误！", map);
		}

		log.debug("音频删除结束");
		return new CustomResult(1, "", map);
	}

}
